package com.cuiweiyou.rootapp;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.cuiweiyou.middleman.IPluginActivity;
import com.cuiweiyou.middleman.PluginManager;

/*
 * 替身Activity。通过这个aty跳转到插件的aty。
 * 必须注册到宿主的Manifest。
 */
public class GhostActivity extends AppCompatActivity {
    
    private IPluginActivity pluginActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("ard", "替身：onCreate()");
        super.onCreate(savedInstanceState);
    
        String className = getIntent().getStringExtra("className");
        Log.e("ard", "替身：拿到插件aty：" + className);
        try {
            Class<?> aClass = PluginManager.getInstance().getDexClassLoader().loadClass(className);
            Object instance = aClass.newInstance();
            if (instance instanceof IPluginActivity){
                pluginActivity = (IPluginActivity) instance;
                pluginActivity.attach(this);
                Bundle bundle = new Bundle();
                pluginActivity.onCreate(bundle);
                Log.e("ard", "替身：插件aty是IPluginActivityLifecycle");
            }else{
                Log.e("ard", "替身：插件aty 不是 IPluginActivityLifecycle");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void setContentView(int layoutResID) {
        Log.e("ard", "替身：super.setContentView(layoutResID)");
        super.setContentView(layoutResID);
        // pluginActivity.setContentView(layoutResID); // 死循环
    }
    
    @Override
    public void setContentView(View view) {
        Log.e("ard", "替身：super.setContentView(view)");
        super.setContentView(view);
        // pluginActivity.setContentView(view); // 死循环
    }
    
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        Log.e("ard", "替身：onSaveInstanceState(outState)");
        super.onSaveInstanceState(outState);
        pluginActivity.onSaveInstanceState(outState);
    }
    
    @Override
    protected void onStart() {
        Log.e("ard", "替身：onStart()");
        super.onStart();
        pluginActivity.onStart();
    }
    
    
    public void onRestart() {
        Log.e("ard", "替身：onRestart()");
        super.onRestart();
        pluginActivity.onRestart();
    }
    
    public void onResume() {
        Log.e("ard", "替身：onResume()");
        super.onResume();
        pluginActivity.onResume();
    }
    
    public void onPause() {
        Log.e("ard", "替身：onPause()");
        super.onPause();
        pluginActivity.onPause();
    }
    
    public void onStop() {
        Log.e("ard", "替身：onStop()");
        super.onStop();
        pluginActivity.onStop();
    }
    
    public void onDestroy() {
        Log.e("ard", "替身：onDestroy()");
        super.onDestroy();
        pluginActivity.onDestroy();
    }
    
    @Override
    public ClassLoader getClassLoader() {
        Log.e("ard", "替身：getClassLoader()");
        return PluginManager.getInstance().getDexClassLoader();
    }
    
    @Override
    public Resources getResources() {
        Log.e("ard", "替身：getResources()");
        return PluginManager.getInstance().getResources();
    }
    
    @Override
    public void startActivity(Intent intent) {
        Log.e("ard", "替身：startActivity(i)");
        String className = intent.getStringExtra("className");
        Intent i = new Intent(this, GhostActivity.class);
        i.putExtra("className", className);
        super.startActivity(i);
    }
}
